package _20同步方法_重点;

public class Ticket implements Runnable{
    private int total=100;
    static Object obj = new Object();
    @Override
    public void run() {
        sale();
        }

    private synchronized void sale(){
        while (true){
        String name = Thread.currentThread().getName();
        System.out.println(name+"抢到锁了，开始购票");
        if (total > 0) {
            total = total - 1;
            System.out.println(name + "购票成功，剩余票数为：" + total);
        }else {
            System.out.println("没票了，下次再来");
            break;
        }
    }
    }

}
